﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CNKI.Graduate.Framework.Ioc;

namespace CNKI.Graduate.Framework.Cache
{
    [ServiceImplementation(Name = CacheHelper.RedisCacheKey)]
    public class RedisCache : IEntityCache
    {
        public bool Set(string key, string entity, TimeSpan exp)
        {
            var db = RedisClient.Manager.GetDatabase();
            return db.StringSet(key, entity, exp);
        }

        public bool Set(string key, string entity)
        {
            var db = RedisClient.Manager.GetDatabase();
            return db.StringSet(key, entity);
        }

        public bool Set<T>(string key, T entity, TimeSpan exp) where T : class
        {
            var db = RedisClient.Manager.GetDatabase();
            string value = JsonHelper.ToJson(entity);
            return db.StringSet(key, value, exp);
        }

        public bool Set<T>(string key, T entity) where T : class
        {
            var db = RedisClient.Manager.GetDatabase();
            string value = JsonHelper.ToJson(entity);
            return db.StringSet(key, value);
        }

        public T Get<T>(string key) where T : class
        {
            var db = RedisClient.Manager.GetDatabase();

            var value = db.StringGet(key);
            return string.IsNullOrEmpty(value) ? null : JsonHelper.ToObject<T>(value);
        }

        public string Get(string key)
        {
            var db = RedisClient.Manager.GetDatabase();
            return db.StringGet(key);
        }

        public bool Remove(string key)
        {
            var db = RedisClient.Manager.GetDatabase();
            return db.KeyDelete(key);
        }


    }
}
